/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestDateFormat.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package api.text;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * java.text.DateFormat测试
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestDateFormat {

	/**
	 * 格式化日期
	 */
	public static void testFormatDate() {
		Date today = new Date();
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
		System.out.println("Date format with short: " + dateFormat.format(today));
		dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM); // 默认
		System.out.println("Date format with medium: " + dateFormat.format(today));
		dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
		System.out.println("Date format with long: " + dateFormat.format(today));
		dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
		System.out.println("Date format with full: " + dateFormat.format(today));
		System.out.println();
	}

	/**
	 * 格式化时间
	 */
	public static void testFormatTime() {
		Date today = new Date();
		DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
		System.out.println("Time format with short: " + dateFormat.format(today));
		dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM); // 默认
		System.out.println("Time format with medium: " + dateFormat.format(today));
		dateFormat = DateFormat.getTimeInstance(DateFormat.LONG);
		System.out.println("Time format with long: " + dateFormat.format(today));
		dateFormat = DateFormat.getTimeInstance(DateFormat.FULL);
		System.out.println("Time format with full: " + dateFormat.format(today));
		System.out.println();
	}

	/**
	 * 格式化日期时间
	 */
	public static void testFormatDateTime() {
		Date today = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT,
				Locale.getDefault());
		System.out.println("DateTime format: " + dateFormat.format(today));
		System.out.println();
	}

	/**
	 * 将字符串转换为日期类型
	 */
	public static void parseStringToDate() {
		String dateString = "2015-11-15 15:30:30";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = format.parse(dateString);
			System.out.println(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 其他方法<br/>
	 */
	@SuppressWarnings("unused")
	public static void testOtherMethods() {
		// 获得本机所有可用的Locale
		Locale[] locales = DateFormat.getAvailableLocales();
		System.out.println("本机一共有" + locales.length + "Locole可用");

		// 获得其他类对象
		Calendar calendar = DateFormat.getInstance().getCalendar();
		TimeZone timeZone = DateFormat.getInstance().getTimeZone();
		NumberFormat numberFormat = DateFormat.getInstance().getNumberFormat();
	}

	public static void main(String[] args) {
		testFormatDate();
		testFormatTime();
		testFormatDateTime();
		parseStringToDate();
		testOtherMethods();
	}
}
